/*
* License:  This  program  is  free  software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by
* the  Free Software Foundation; either version 3 of the License, or (at your
* option)  any later version. This program is distributed in the hope that it
* will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*/

package pcgen.CharacterViewer.fragments;

import java.util.ArrayList;

import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ListView;

import pcgen.CharacterViewer.R;
import pcgen.CharacterViewer.resources.GameFolder;
import pcgen.CharacterViewer.resources.GameResourceCondition;

public class ConditionDialogFragment extends CharacterTabContentDialogFragment 
{
	public static ConditionDialogFragment newInstance(CharacterTabContentFragment fragment, Types type) 
    {
        ConditionDialogFragment dialog = new ConditionDialogFragment();

        // Pass args to dialog fragment...
        Bundle args = new Bundle();
        args.putString("type", type.toString());
        dialog.setArguments(args);
        
        initFragment(dialog, fragment);
	    
	    return dialog;
    }
	
	public boolean doDialogNegativeClick()
	{
		return getTargetFragmentEx().dialogCancelCondition();
	}
	
	public boolean doDialogPositiveClick()
	{
		for (ListItemCondition condition : _conditions)
		{
			if (!condition.Checked)
				continue;
			
			if (_type == Types.Add)
				getCharacter().setCondition(condition.Condition.getConditionType());
			else
				getCharacter().removeCondition(condition.Condition.getConditionType());
		}
		
		return getTargetFragmentEx().dialogCompleteCondition();
	}
    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        // Get args...
        _type = Types.valueOf(getArguments().getString("type"));
        _title = (isRemove() ? R.string.dialog_damage_heal_title_damage : R.string.dialog_damage_heal_title_heal);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) 
    {
    	final DialogFragment reference = this;
    	
        View view = inflater.inflate(R.layout.dialog_condition, container, false);
        
        getDialog().setTitle(getString(_title));
        
        _list = (ListView)view.findViewById(R.id.list_conditions);
        
        _conditions =getConditions();
        ArrayAdapter<ListItemCondition> adapter = new ArrayAdapter<ListItemCondition>(getActivity(), android.R.layout.simple_list_item_multiple_choice, _conditions)
        {
        	@Override
        	public View getView(int position, View convertView, ViewGroup parent) 
        	{
        		CheckedTextView rowView = (CheckedTextView)convertView;
        		if (convertView == null) 
        		{
        			//convertView = new CheckedTextView(reference.getActivity());
        			//label = (CheckedTextView)convertView;
        			rowView = (CheckedTextView)reference.getActivity().getLayoutInflater().inflate(android.R.layout.simple_list_item_multiple_choice, parent, false);
        		}
        		   
        		rowView.setText(getItem(position).Condition.getName());
        		rowView.setPadding(10, 0, 0, 0);
        		rowView.setTextSize(23);
        		rowView.setChecked(false);
        		
        		return rowView;
        	}
        };
        _list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        _list.setAdapter(adapter);
        _list.setOnItemClickListener(new OnItemClickListener()
        {
			public void onItemClick(AdapterView<?> parent, View view, int pos, long id) 
			{
				ListItemCondition condition = _conditions.get(pos);
				condition.Checked = !condition.Checked;
			}
        });
        
        _buttonOk = (Button)view.findViewById(R.id.button_ok);
        _buttonOk.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
            	if (doDialogPositiveClick())
            		reference.dismiss();
            }
        });
        
        _buttonCancel = (Button)view.findViewById(R.id.button_cancel);
        _buttonCancel.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
            	if (doDialogNegativeClick())
            		reference.dismiss();
            }
        });

        return view;
    }
    
    private boolean isAdd()
    {
    	return (_type == Types.Add);
    }
    
    private boolean isRemove()
    {
    	return (_type == Types.Remove);
    }
    
    private ArrayList<ListItemCondition> getConditions()
    {
    	GameFolder folder = getCharacter().getFolder();
		ArrayList<GameResourceCondition> conditionTypes = folder.getTypeConditions();
		ArrayList<GameResourceCondition> currentConditions = getCharacter().getConditionalModifiers();
		
    	ArrayList<ListItemCondition> items = new ArrayList<ListItemCondition>();
		if (_type == Types.Add)
		{
			ArrayList<GameResourceCondition> list = getCharacter().getConditionalModifiers();
			for (GameResourceCondition condition : conditionTypes)
			{
				if (!folder.isConditionSettable(condition.getConditionType()))
					continue;
				
				boolean found = false;
				for (GameResourceCondition search : currentConditions)
				{
					if (search.getConditionType() == condition.getConditionType())
					{
						found = true;
						break;
					}
				}
				
				if (found)
					continue;
				
				condition.setCharacter(getCharacter());
				ListItemCondition item = new ListItemCondition(condition);
				items.add(item);
			}
		}
		else if (_type == Types.Remove)
		{
			for (GameResourceCondition condition : currentConditions)
			{
				if (!folder.isConditionClearable(condition.getConditionType()))
					continue;

				condition.setCharacter(getCharacter());
				ListItemCondition item = new ListItemCondition(condition);
				items.add(item);
			}
		}
		
		return items;
    }
    
	private int _title;
	private Types _type;
	
	private ArrayList<ListItemCondition> _conditions;
	
	private ListView _list;
	
	private Button _buttonCancel;
	private Button _buttonOk;
	
	public enum Types
	{
		Add,
		Remove
	}

	private static final String TAG = ConditionDialogFragment.class.getSimpleName();
	
	public class ListItemCondition
	{
		public ListItemCondition(GameResourceCondition condition)
		{
			Condition = condition;
		}
		
		public GameResourceCondition Condition;
		public boolean Checked;
	}
}
